<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增参数' : '编辑参数'" append-to-body width="400px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="所属分类：" prop="path_name">
        <span>{{ form.path_name }}</span>
      </el-form-item>
      <el-form-item label="参数名称：" prop="parameter_name">
        <el-input v-model="form.parameter_name" style="width: 200px;"/>
      </el-form-item>
      <el-form-item label="排序：" prop="parameter_sort">
        <el-input v-model.number="form.parameter_sort" style="width: 200px;"/>
      </el-form-item>
      <el-form-item label="是否必填：" prop="is_required">
        <el-radio v-model="form.is_required" label="true">是</el-radio>
        <el-radio v-model="form.is_required" label="false">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="doSubmit('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/mall/parameter'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    const checkSort = (rule, parameter_sort, callback) => {
      if (parameter_sort === '' || parameter_sort === undefined) {
        callback()
      } else if (!/^[0-9]+$/.test(parameter_sort) || parameter_sort === 0) {
        callback(new Error('排序必须是大于0的整数'))
      } else {
        callback()
      }
    }
    return {
      dialog: false, loading: false, form: { parameter_name: '', parameter_sort: '', is_required: 'false', variety_id: '', path_name: '' },
      is_required: false,
      rules: {
        parameter_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        parameter_sort: [
          { validator: checkSort, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { parameter_name: '', parameter_sort: '', is_required: 'false', variety_id: '', path_name: '' }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        if (res.result) {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        if (res.result) {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
